local godHuishi = fk.CreateSkill {

  name = "joy__god_huishi",

  tags = {  },

}



godHuishi:addEffect("active", {
  name = "joy__god_huishi",
  anim_type = "drawcard",
  can_use = function(self, player)
    return player:usedSkillTimes(godHuishi.name, Player.HistoryPhase) == 0 and player.maxHp < 10
  end,
  card_filter = function(self, player, to_select, selected)
    return false
  end,
  target_filter = function(self, player, to_select, selected)
    return false
  end,
  on_use = function(self, room, effect)
    local from = effect.from
    local cardsJudged = {}
    while from.maxHp < 10 do
      local parsePattern = table.concat(table.map(cardsJudged, function(card)
        return card:getSuitString()
      end), ",")
      local judge = {
        who = from,
        reason = godHuishi.name,
        pattern = ".|.|" .. (parsePattern == "" and "." or "^(" .. parsePattern .. ")"),
        skipDrop = true,
      }
      room:judge(judge)
      table.insert(cardsJudged, judge.card)
      if
        not table.every(cardsJudged, function(card)
          return card == judge.card or judge.card:compareSuitWith(card, true)
        end) or
        not room:askForSkillInvoke(from, godHuishi.name, nil, "#mobile__god_huishi-ask")
      then
        break
      end
      room:changeMaxHp(from, 1)
    end
    local alivePlayerIds = table.map(room.alive_players, function(p)
      return p.id
    end)
    cardsJudged = table.filter(cardsJudged, function(card)
      return room:getCardArea(card.id) == Card.Processing
    end)
    if #cardsJudged == 0 then
      return false
    end
    local targets = room:askForChoosePlayers(from, alivePlayerIds, 1, 1, "#mobile__god_huishi-give", godHuishi.name, true)
    if #targets > 0 then
      local to = targets[1]
      local pack = Fk:cloneCard("slash")
      pack:addSubcards(table.map(cardsJudged, function(card)
        return card:getEffectiveId()
      end))
      room:obtainCard(to, pack, true, fk.ReasonGive)
      if
        table.every(room.alive_players, function(p)
          return p:getHandcardNum() <= room:getPlayerById(to):getHandcardNum()
        end)
      then
        room:changeMaxHp(from, -1)
      end
    else
      room:moveCards({
        ids = table.map(cardsJudged, function(card)
          return card:getEffectiveId()
        end),
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonPutIntoDiscardPile,
        skillName = godHuishi.name,
      })
    end
  end,
})

return godHuishi